var publicJs = require("../../public.js");
var interval = null; //倒计时函数

Page({
  data: {
    mobile: '',
    password: '',
    code: '',
    sendcode: '',
    time: '获取验证码', //倒计时 
    currentTime: 60
  },
  mobileChange: function (e) {
    //手机号
    this.data.mobile = e.detail.value
  }, 
  passwordChange: function (e) {
    //密码
    this.data.password = e.detail.value
  },
  codeChange: function (e) {
    //验证码
    this.data.code = e.detail.value
  },
  getCode: function (options) {
    var that = this;
    var currentTime = that.data.currentTime
    //获取验证码
    if (!publicJs.isTelphone(this.data.mobile)) {
      wx.showModal({
        title: '错误',
        content: '请填写正确的手机号！',
        showCancel: false,
        success: function (res) {}
      })
      return false;
    }
    that.setData({
      disabled: false
    });
    publicJs.JsonPost("Identify/getEmsCode", {
      mobile: this.data.mobile,
      type: 2
    }, function (data) {
      console.log(data);
      if (data === false) {
        that.setData({
          disabled: true
        })
        return;
      }
      console.log('获取短信验证码' + JSON.stringify(data));
      that.data.sendcode = data;
      interval = setInterval(function () {
        currentTime--;
        that.setData({
          time: '重新发送(' + currentTime + 's)'
        })
        if (currentTime <= 0) {
          clearInterval(interval)
          that.setData({
            time: '重新发送',
            currentTime: 61,
            disabled: false
          })
        }
      }, 1000)
    })
  },
  getVerificationCode() {
    this.getCode();
    var that = this
    that.setData({
      disabled: true
    })
  },
  doLogin: function (){
    publicJs.JsonPost("Personal/login", {
      user_name: this.data.mobile,
      user_pwd: this.data.password,
      needIdentify: 1,
      identify: this.data.code,
      sendIdentify: this.data.sendcode
    },function(data){
      publicJs.showLoading("登录中...请稍后...");
      console.log(data);

      if(data !== false){
        //缓存用户信息
        wx.setStorageSync("userId", data.id);
        wx.setStorageSync("userName", data.user_name);
        wx.setStorageSync("realName", data.real_name);
        wx.setStorageSync("mobile", data.mobile);
        wx.setStorageSync("avatar", data.avatar);
        wx.setStorageSync("userData", JSON.stringify(data));
        
        publicJs.closeLoading();
        wx.switchTab({
          url: '../index/index'
        })
      }
      
    })
  },
  register: function () {
    //注册
    wx.navigateTo({ url: "../reg/reg" })
  }
})


